package cn.iocoder.yudao.module.bill.controller.admin.bill;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportRespVO;
import cn.iocoder.yudao.module.system.enums.common.SexEnum;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bill.controller.admin.bill.vo.*;
import cn.iocoder.yudao.module.bill.dal.dataobject.bill.BillDO;
import cn.iocoder.yudao.module.bill.service.bill.BillService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 账单信息")
@RestController
@RequestMapping("/bill/bill")
@Validated
public class BillController {

    @Resource
    private BillService billService;



    @PostMapping("/create")
    @Operation(summary = "创建账单信息")
    @PreAuthorize("@ss.hasPermission('bill:bill:create')")
    public CommonResult<Long> createBill(@Valid @RequestBody BillSaveReqVO createReqVO) {
        return success(billService.createBill(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单信息")
    @PreAuthorize("@ss.hasPermission('bill:bill:update')")
    public CommonResult<Boolean> updateBill(@Valid @RequestBody BillSaveReqVO updateReqVO) {
        billService.updateBill(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-batch")
    @Operation(summary = "更新账单信息")
    @PreAuthorize("@ss.hasPermission('bill:bill:update')")
    public CommonResult<Boolean> updateBillBatch(@RequestParam("ids") Long[] ids,@RequestBody BillSaveReqVO updateReqVO) {
        billService.updateBatchBill(List.of(ids),updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bill:bill:delete')")
    public CommonResult<Boolean> deleteBill(@RequestParam("id") Long id) {
        billService.deleteBill(id);
        return success(true);
    }

    @DeleteMapping("/delete-batch")
    @Operation(summary = "批量删除账单信息")
    @PreAuthorize("@ss.hasPermission('bill:bill:delete')")
    public CommonResult<Boolean> deleteBills(@RequestParam("ids") Long[] ids) {
        billService.deleteBatchBill(List.of(ids));
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bill:bill:query')")
    public CommonResult<BillRespVO> getBill(@RequestParam("id") Long id) {
        BillDO bill = billService.getBill(id);
        return success(BeanUtils.toBean(bill, BillRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单信息分页")
    @PreAuthorize("@ss.hasPermission('bill:bill:query')")
    public CommonResult<PageResult<BillRespVO>> getBillPage(@Valid BillPageReqVO pageReqVO) {
        PageResult<BillDO> pageResult = billService.getBillPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账单信息 Excel")
    @PreAuthorize("@ss.hasPermission('bill:bill:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBillExcel(@Valid BillPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillDO> list = billService.getBillPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账单信息.xls", "数据", BillRespVO.class,
                        BeanUtils.toBean(list, BillRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得账单导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<BillImportVO> list = new ArrayList<>();

        list.add(
                new BillImportVO("这是账单标题-不能为空","这是账单备注",
                        BigDecimal.valueOf(9.99),"不计收支",10003L,"餐饮美食","一日三餐",
                        "标签1@标签2",null,"支付账单","工作","审核通过",0,null,
                        LocalDateTime.now(),null,null,null,null,null,null,null,null,null,null,null,null,null,null)
        );

        ExcelUtils.write(response, "账单导入模板.xls", "账单列表", BillImportVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入账单")
    public CommonResult<BillImportVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<BillImportVO> list = ExcelUtils.read(file, BillImportVO.class);
        return success(billService.importList(list));
    }

}